// #include <iostream>
// #include <string>
// #include <vector>
// #include <pthread.h>
// #include <semaphore.h>
// #include <cstdlib>
// #include <ctime>

// struct Family {
//     std::string _name;
//     size_t _money;
//     int _times;
//     std::string _transaction;

//     Family(const std::string& name, size_t money, int times = 2)
//         : _name(name),
//           _money(money),
//           _times(times)
//     {}
// };

// // 初始化锁
// pthread_mutex_t pmutex = PTHREAD_MUTEX_INITIALIZER;

// int money = 10;

// // 存钱
// void* deposit(void* args) {
//     Family* person = static_cast<Family*>(args);
//     // 在这里面取钱，对当前线程进行加锁
//     pthread_mutex_lock(&pmutex);
//     money += person->_money;
//     std::cout << person->_transaction << ": " << person->_name << " deposit "
//         << person->_money << " yuans, " << "the account's money is: " << money << std::endl;  
//     pthread_mutex_unlock(&pmutex);
//     return nullptr;
// }

// void* withdraw(void* args) {
//     Family* person = static_cast<Family*>(args);
//     // 在这里取钱
//     pthread_mutex_lock(&pmutex);
//     money -= person->_money;
//     std::cout << person->_transaction << ": " << person->_name << " withdraw "
//         << person->_money << " yuans, " << "the account's money is: " << money << std::endl;    
//     pthread_mutex_unlock(&pmutex);
//     return nullptr;
// }

// int main() {

//     Family father("father", 10), mother("mother", 20), granma("granma", 30);
//     Family granpa("granpa", 40), uncle("uncle", 50);
//     Family mary("mary", 50), sally("sally", 100);

//     std::vector<Family> family;
//     family.push_back(father), family.push_back(mother), family.push_back(granma);
//     family.push_back(granpa), family.push_back(uncle), family.push_back(mary), family.push_back(sally);
    
//     // 设计随机数种子
//     srand(time(0));
    
//     std::vector<pthread_t> threads;
//     size_t times = 1;

//     while (!family.empty()) {
//         size_t index = rand() % family.size();
//         // 若当前的次数已经消耗殆尽，则删除对应的元素
//         if (family[index]._times == 0) {
//            auto it = family.begin() + index;
//         //    std::cout << "删除一个元素:" << std::endl; 

//            family.erase(it);
//            continue;
//         }

//         family[index]._times--;

//         // 记录当前是第几次交易
//         family[index]._transaction = "transaction-" + std::to_string(times);
//         // 判断当前选中的角色
//         if (family[index]._name == "mary" || family[index]._name == "sally") {
//             pthread_t thread;
//             threads.push_back(thread);
//             pthread_create(&thread, nullptr, withdraw, (void*)(&family[index]));
//         } else {
//             pthread_t thread;
//             threads.push_back(thread);    
//             pthread_create(&thread, nullptr, deposit, (void*)(&family[index]));
//         }
//         times++;
//     }

//     std::cout << "finally, the account's money is: " << money << std::endl;
//     for (auto& thread : threads)
//         pthread_join(thread, nullptr);
//     return 0;
// }
